
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Message Intercepting · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="supported-parameters-annotated-handlers.html" />
    
    
    <link rel="prev" href="message-correlation.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../architecture-overview/">
            
                <a href="../../architecture-overview/">
            
                    
                    Architecture Overview
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../architecture-overview/ddd-cqrs-concepts.html">
            
                <a href="../../architecture-overview/ddd-cqrs-concepts.html">
            
                    
                    DDD & CQRS Concepts
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../architecture-overview/event-sourcing.html">
            
                <a href="../../architecture-overview/event-sourcing.html">
            
                    
                    Event Sourcing
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../architecture-overview/event-driven-microservices.html">
            
                <a href="../../architecture-overview/event-driven-microservices.html">
            
                    
                    Event-Driven Microservices
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../axon-server-introduction.html">
            
                <a href="../../axon-server-introduction.html">
            
                    
                    Axon Server
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../release-notes/">
            
                <a href="../../release-notes/">
            
                    
                    Release Notes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../release-notes/rn-axon-framework/">
            
                <a href="../../release-notes/rn-axon-framework/">
            
                    
                    Axon Framework
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1.1" data-path="../../release-notes/rn-axon-framework/rn-af-major-releases.html">
            
                <a href="../../release-notes/rn-axon-framework/rn-af-major-releases.html">
            
                    
                    Major Releases
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.1.2" data-path="../../release-notes/rn-axon-framework/rn-af-minor-releases.html">
            
                <a href="../../release-notes/rn-axon-framework/rn-af-minor-releases.html">
            
                    
                    Minor Releases
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../../release-notes/rn-axon-server/">
            
                <a href="../../release-notes/rn-axon-server/">
            
                    
                    Axon Server
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.2.1" data-path="../../release-notes/rn-axon-server/rn-as-major-releases.html">
            
                <a href="../../release-notes/rn-axon-server/rn-as-major-releases.html">
            
                    
                    Major Releases
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.2" data-path="../../release-notes/rn-axon-server/rn-asse-minor-releases.html">
            
                <a href="../../release-notes/rn-axon-server/rn-asse-minor-releases.html">
            
                    
                    Minor Releases Standard Edition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.3" data-path="../../release-notes/rn-axon-server/rn-as-minor-releases.html">
            
                <a href="../../release-notes/rn-axon-server/rn-as-minor-releases.html">
            
                    
                    Minor Releases Enterprise Edition
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../../release-notes/axon-framework-extensions.html">
            
                <a href="../../release-notes/axon-framework-extensions.html">
            
                    
                    Axon Framework Extensions
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">Getting Started</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../getting-started/quick-start.html">
            
                <a href="../../getting-started/quick-start.html">
            
                    
                    Quick Start
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Axon Framework</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../introduction.html">
            
                <a href="../introduction.html">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="./">
            
                <a href="./">
            
                    
                    Messaging Concepts
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="anatomy-message.html">
            
                <a href="anatomy-message.html">
            
                    
                    Anatomy of a Message
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="message-correlation.html">
            
                <a href="message-correlation.html">
            
                    
                    Message Correlation
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.2.3" data-path="message-intercepting.html">
            
                <a href="message-intercepting.html">
            
                    
                    Message Intercepting
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4" data-path="supported-parameters-annotated-handlers.html">
            
                <a href="supported-parameters-annotated-handlers.html">
            
                    
                    Supported Parameters for Annotated Handlers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.5" data-path="exception-handling.html">
            
                <a href="exception-handling.html">
            
                    
                    Exception Handling
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.6" data-path="unit-of-work.html">
            
                <a href="unit-of-work.html">
            
                    
                    Unit of Work
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../axon-framework-commands/">
            
                <a href="../axon-framework-commands/">
            
                    
                    Commands
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="../axon-framework-commands/modeling/">
            
                <a href="../axon-framework-commands/modeling/">
            
                    
                    Modeling
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1.1" data-path="../axon-framework-commands/modeling/aggregate.html">
            
                <a href="../axon-framework-commands/modeling/aggregate.html">
            
                    
                    Aggregate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.1.2" data-path="../axon-framework-commands/modeling/multi-entity-aggregates.html">
            
                <a href="../axon-framework-commands/modeling/multi-entity-aggregates.html">
            
                    
                    Multi-Entity Aggregates
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.1.3" data-path="../axon-framework-commands/modeling/state-stored-aggregates.html">
            
                <a href="../axon-framework-commands/modeling/state-stored-aggregates.html">
            
                    
                    State Stored Aggregates
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.1.4" data-path="../axon-framework-commands/modeling/aggregate-creation-from-another-aggregate.html">
            
                <a href="../axon-framework-commands/modeling/aggregate-creation-from-another-aggregate.html">
            
                    
                    Aggregate Creation from another Aggregate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.1.5" data-path="../axon-framework-commands/modeling/aggregate-polymorphism.html">
            
                <a href="../axon-framework-commands/modeling/aggregate-polymorphism.html">
            
                    
                    Aggregate Polymorphism
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.1.6" data-path="../axon-framework-commands/modeling/conflict-resolution.html">
            
                <a href="../axon-framework-commands/modeling/conflict-resolution.html">
            
                    
                    Conflict Resolution
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="../axon-framework-commands/command-dispatchers.html">
            
                <a href="../axon-framework-commands/command-dispatchers.html">
            
                    
                    Command Dispatchers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="../axon-framework-commands/command-handlers.html">
            
                <a href="../axon-framework-commands/command-handlers.html">
            
                    
                    Command Handlers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="../axon-framework-commands/infrastructure.html">
            
                <a href="../axon-framework-commands/infrastructure.html">
            
                    
                    Infrastructure
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.5" data-path="../axon-framework-commands/configuration.html">
            
                <a href="../axon-framework-commands/configuration.html">
            
                    
                    Configuration
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../events/">
            
                <a href="../events/">
            
                    
                    Events
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.4.1" data-path="../events/event-dispatchers.html">
            
                <a href="../events/event-dispatchers.html">
            
                    
                    Event Dispatchers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.2" data-path="../events/event-handlers.html">
            
                <a href="../events/event-handlers.html">
            
                    
                    Event Handlers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.3" data-path="../events/event-processors/">
            
                <a href="../events/event-processors/">
            
                    
                    Event Processors
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.4.3.1" data-path="../events/event-processors/subscribing.html">
            
                <a href="../events/event-processors/subscribing.html">
            
                    
                    Subscribing Event Processors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.3.2" data-path="../events/event-processors/streaming.html">
            
                <a href="../events/event-processors/streaming.html">
            
                    
                    Streaming Event Processors
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4.4" data-path="../events/event-bus-and-event-store.html">
            
                <a href="../events/event-bus-and-event-store.html">
            
                    
                    Event Bus & Event Store
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.5" data-path="../events/event-versioning.html">
            
                <a href="../events/event-versioning.html">
            
                    
                    Event Versioning
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.6" data-path="../events/event-serialization.html">
            
                <a href="../events/event-serialization.html">
            
                    
                    Event Serialization
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../queries/">
            
                <a href="../queries/">
            
                    
                    Queries
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.5.1" data-path="../queries/query-processing.html">
            
                <a href="../queries/query-processing.html">
            
                    
                    Query Processing
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.2" data-path="../queries/query-dispatchers.html">
            
                <a href="../queries/query-dispatchers.html">
            
                    
                    Query Dispatchers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.3" data-path="../queries/query-handlers.html">
            
                <a href="../queries/query-handlers.html">
            
                    
                    Query Handlers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.4" data-path="../queries/implementations.html">
            
                <a href="../queries/implementations.html">
            
                    
                    Implementations
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.5" data-path="../queries/configuration.html">
            
                <a href="../queries/configuration.html">
            
                    
                    Configuration
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../sagas/">
            
                <a href="../sagas/">
            
                    
                    Sagas
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.6.1" data-path="../sagas/implementation.html">
            
                <a href="../sagas/implementation.html">
            
                    
                    Implementation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.2" data-path="../sagas/associations.html">
            
                <a href="../sagas/associations.html">
            
                    
                    Associations
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.3" data-path="../sagas/infrastructure.html">
            
                <a href="../sagas/infrastructure.html">
            
                    
                    Infrastructure
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../deadlines/">
            
                <a href="../deadlines/">
            
                    
                    Deadlines
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.7.1" data-path="../deadlines/deadline-managers.html">
            
                <a href="../deadlines/deadline-managers.html">
            
                    
                    Deadline Managers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.2" data-path="../deadlines/event-schedulers.html">
            
                <a href="../deadlines/event-schedulers.html">
            
                    
                    Event Schedulers
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../testing/">
            
                <a href="../testing/">
            
                    
                    Testing
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.8.1" data-path="../testing/commands-events.html">
            
                <a href="../testing/commands-events.html">
            
                    
                    Commands / Events
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8.2" data-path="../testing/sagas-1.html">
            
                <a href="../testing/sagas-1.html">
            
                    
                    Sagas
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="../tuning/">
            
                <a href="../tuning/">
            
                    
                    Tuning
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.9.1" data-path="../tuning/event-snapshots.html">
            
                <a href="../tuning/event-snapshots.html">
            
                    
                    Event Snapshots
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.9.2" data-path="../tuning/event-processing.html">
            
                <a href="../tuning/event-processing.html">
            
                    
                    Event Processing
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.9.3" data-path="../tuning/command-processing.html">
            
                <a href="../tuning/command-processing.html">
            
                    
                    Command Processing
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="../monitoring-and-metrics.html">
            
                <a href="../monitoring-and-metrics.html">
            
                    
                    Monitoring and Metrics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.11" data-path="../spring-boot-integration.html">
            
                <a href="../spring-boot-integration.html">
            
                    
                    Spring Boot Integration
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.12" data-path="../modules.html">
            
                <a href="../modules.html">
            
                    
                    Modules
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Axon Server</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../axon-server/introduction.html">
            
                <a href="../../axon-server/introduction.html">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../../axon-server/installation/">
            
                <a href="../../axon-server/installation/">
            
                    
                    Installation
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.2.1" data-path="../../axon-server/installation/local-installation/">
            
                <a href="../../axon-server/installation/local-installation/">
            
                    
                    Local Installation
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.2.1.1" data-path="../../axon-server/installation/local-installation/axon-server-se.html">
            
                <a href="../../axon-server/installation/local-installation/axon-server-se.html">
            
                    
                    Axon Server SE
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.1.2" data-path="../../axon-server/installation/local-installation/axon-server-ee.html">
            
                <a href="../../axon-server/installation/local-installation/axon-server-ee.html">
            
                    
                    Axon Server EE
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.2.2" data-path="../../axon-server/installation/docker-k8s/">
            
                <a href="../../axon-server/installation/docker-k8s/">
            
                    
                    Docker / K8s
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.2.2.1" data-path="../../axon-server/installation/docker-k8s/axon-server-se.html">
            
                <a href="../../axon-server/installation/docker-k8s/axon-server-se.html">
            
                    
                    Axon Server SE
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.2.2" data-path="../../axon-server/installation/docker-k8s/axon-server-ee.html">
            
                <a href="../../axon-server/installation/docker-k8s/axon-server-ee.html">
            
                    
                    Axon Server EE
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../../axon-server/administration/">
            
                <a href="../../axon-server/administration/">
            
                    
                    Administration
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.3.1" data-path="../../axon-server/administration/admin-configuration/">
            
                <a href="../../axon-server/administration/admin-configuration/">
            
                    
                    Configuration
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.3.1.1" data-path="../../axon-server/administration/admin-configuration/configuration.html">
            
                <a href="../../axon-server/administration/admin-configuration/configuration.html">
            
                    
                    System Properties
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.1.2" data-path="../../axon-server/administration/admin-configuration/command-line-interface.html">
            
                <a href="../../axon-server/administration/admin-configuration/command-line-interface.html">
            
                    
                    Command Line Interface
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.1.3" data-path="../../axon-server/administration/admin-configuration/rest-api.html">
            
                <a href="../../axon-server/administration/admin-configuration/rest-api.html">
            
                    
                    REST API
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.3.2" data-path="../../axon-server/administration/monitoring/">
            
                <a href="../../axon-server/administration/monitoring/">
            
                    
                    Monitoring
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.3.2.1" data-path="../../axon-server/administration/monitoring/actuator-endpoints.html">
            
                <a href="../../axon-server/administration/monitoring/actuator-endpoints.html">
            
                    
                    Actuator Endpoints
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.2.2" data-path="../../axon-server/administration/monitoring/grpc-metrics.html">
            
                <a href="../../axon-server/administration/monitoring/grpc-metrics.html">
            
                    
                    gRPC Metrics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.2.3" data-path="../../axon-server/administration/monitoring/heartbeat-monitoring.html">
            
                <a href="../../axon-server/administration/monitoring/heartbeat-monitoring.html">
            
                    
                    Heartbeat Monitoring
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.3.3" data-path="../../axon-server/administration/clustering.html">
            
                <a href="../../axon-server/administration/clustering.html">
            
                    
                    Clusters
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.4" data-path="../../axon-server/administration/replication-groups.html">
            
                <a href="../../axon-server/administration/replication-groups.html">
            
                    
                    Replication Groups
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.5" data-path="../../axon-server/administration/multi-context.html">
            
                <a href="../../axon-server/administration/multi-context.html">
            
                    
                    Multi-Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.6" data-path="../../axon-server/administration/tagging.html">
            
                <a href="../../axon-server/administration/tagging.html">
            
                    
                    Tagging
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.7" data-path="../../axon-server/administration/backup-and-messaging-only-nodes.html">
            
                <a href="../../axon-server/administration/backup-and-messaging-only-nodes.html">
            
                    
                    Backup and Messaging-only Nodes
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.8" data-path="../../axon-server/administration/backups.html">
            
                <a href="../../axon-server/administration/backups.html">
            
                    
                    Backups
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.9" data-path="../../axon-server/administration/recovery.html">
            
                <a href="../../axon-server/administration/recovery.html">
            
                    
                    Recovery
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.10" data-path="../../axon-server/administration/plugins.html">
            
                <a href="../../axon-server/administration/plugins.html">
            
                    
                    Plugins
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3.11" data-path="../../axon-server/administration/error-codes.html">
            
                <a href="../../axon-server/administration/error-codes.html">
            
                    
                    Error Codes
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../../axon-server/security/">
            
                <a href="../../axon-server/security/">
            
                    
                    Security
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.1" data-path="../../axon-server/security/ssl.html">
            
                <a href="../../axon-server/security/ssl.html">
            
                    
                    SSL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4.2" data-path="../../axon-server/security/access-control.html">
            
                <a href="../../axon-server/security/access-control.html">
            
                    
                    Access Control
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4.3" data-path="../../axon-server/security/access-control-se.html">
            
                <a href="../../axon-server/security/access-control-se.html">
            
                    
                    Access Control - Standard Edition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4.4" data-path="../../axon-server/security/access-control-ee.html">
            
                <a href="../../axon-server/security/access-control-ee.html">
            
                    
                    Access Control - Enterprise Edition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4.5" data-path="../../axon-server/security/access-control-clients.html">
            
                <a href="../../axon-server/security/access-control-clients.html">
            
                    
                    Access Control - Client Applications
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4.6" data-path="../../axon-server/security/access-control-cli.html">
            
                <a href="../../axon-server/security/access-control-cli.html">
            
                    
                    Access Control - CLI
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4.7" data-path="../../axon-server/security/access-control-api.html">
            
                <a href="../../axon-server/security/access-control-api.html">
            
                    
                    Access Control - API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4.8" data-path="../../axon-server/security/access-control-ldap.html">
            
                <a href="../../axon-server/security/access-control-ldap.html">
            
                    
                    Access Control - LDAP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4.9" data-path="../../axon-server/security/access-control-oauth2.html">
            
                <a href="../../axon-server/security/access-control-oauth2.html">
            
                    
                    Access Control - OAuth 2.0
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../../axon-server/performance/">
            
                <a href="../../axon-server/performance/">
            
                    
                    Performance
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.5.1" data-path="../../axon-server/performance/tuning-event-processing.html">
            
                <a href="../../axon-server/performance/tuning-event-processing.html">
            
                    
                    Event Segments
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5.2" data-path="../../axon-server/performance/flow-control.html">
            
                <a href="../../axon-server/performance/flow-control.html">
            
                    
                    Flow Control
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="../../axon-server/migration/">
            
                <a href="../../axon-server/migration/">
            
                    
                    Migration
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.6.1" data-path="../../axon-server/migration/standard-to-enterprise-edition.html">
            
                <a href="../../axon-server/migration/standard-to-enterprise-edition.html">
            
                    
                    Standard to Enterprise Edition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6.2" data-path="../../axon-server/migration/non-axon-server-to-axon-server.html">
            
                <a href="../../axon-server/migration/non-axon-server-to-axon-server.html">
            
                    
                    Non-Axon Server to Axon Server
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">Extensions</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../../extensions/spring-amqp.html">
            
                <a href="../../extensions/spring-amqp.html">
            
                    
                    Spring AMQP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../../extensions/jgroups.html">
            
                <a href="../../extensions/jgroups.html">
            
                    
                    JGroups
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../../extensions/kafka.html">
            
                <a href="../../extensions/kafka.html">
            
                    
                    Kafka
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../../extensions/kotlin.html">
            
                <a href="../../extensions/kotlin.html">
            
                    
                    Kotlin
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../../extensions/mongo.html">
            
                <a href="../../extensions/mongo.html">
            
                    
                    Mongo
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../../extensions/reactor/reactor.html">
            
                <a href="../../extensions/reactor/reactor.html">
            
                    
                    Reactor
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.6.1" data-path="../../extensions/reactor/reactive-gateways/reactive-gateways.html">
            
                <a href="../../extensions/reactor/reactive-gateways/reactive-gateways.html">
            
                    
                    Reactor Gateways
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="../../extensions/spring-cloud.html">
            
                <a href="../../extensions/spring-cloud.html">
            
                    
                    Spring Cloud
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="../../extensions/tracing.html">
            
                <a href="../../extensions/tracing.html">
            
                    
                    Tracing
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Appendices</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../../appendices/rdbms-tuning.html">
            
                <a href="../../appendices/rdbms-tuning.html">
            
                    
                    A. RDBMS Tuning
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../../appendices/message-handler-tuning/">
            
                <a href="../../appendices/message-handler-tuning/">
            
                    
                    B. Message Handler Tuning
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.2.1" data-path="../../appendices/message-handler-tuning/parameter-resolvers.html">
            
                <a href="../../appendices/message-handler-tuning/parameter-resolvers.html">
            
                    
                    Parameter Resolvers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2.2" data-path="../../appendices/message-handler-tuning/handler-enhancers.html">
            
                <a href="../../appendices/message-handler-tuning/handler-enhancers.html">
            
                    
                    Handler Enhancers
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="../../appendices/meta-annotations.html">
            
                <a href="../../appendices/meta-annotations.html">
            
                    
                    C. Meta Annotations
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="../../appendices/identifier-generation.html">
            
                <a href="../../appendices/identifier-generation.html">
            
                    
                    D. Identifier Generation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="../../appendices/query-reference.html">
            
                <a href="../../appendices/query-reference.html">
            
                    
                    E. Axon Server Query Language
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Message Intercepting</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="message-intercepting">Message Intercepting</h1>
<p>There are two different types of interceptors: dispatch interceptors and handler interceptors. Dispatch interceptors are invoked before a message is dispatched to a message handler. At that point, it may not even be known that a handler exists for that message. Handler interceptors are invoked just before the message handler is invoked.</p>
<h2 id="command-interceptors">Command Interceptors</h2>
<p>One of the advantages of using a command bus is the ability to undertake action based on all incoming commands. Examples are logging or authentication, which you might want to do regardless of the type of command. This is done using Interceptors.</p>
<h3 id="command-dispatch-interceptors">Command Dispatch Interceptors</h3>
<p>Message dispatch interceptors are invoked when a command is dispatched on a command bus. They have the ability to alter the command message by adding metadata. They can also block the command by throwing an exception. These interceptors are always invoked on the thread that dispatches the command.</p>
<p>Let&apos;s create a <code>MessageDispatchInterceptor</code> which logs each command message being dispatched on a <code>CommandBus</code>.</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCommandDispatchInterceptor</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MessageDispatchInterceptor</span>&lt;<span class="hljs-title">CommandMessage</span>&lt;?&gt;&gt; </span>{

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Logger LOGGER = LoggerFactory.getLogger(MyCommandDispatchInterceptor.class);

    <span class="hljs-meta">@Override</span>
    <span class="hljs-keyword">public</span> BiFunction&lt;Integer, CommandMessage&lt;?&gt;, CommandMessage&lt;?&gt;&gt; handle(List&lt;? extends CommandMessage&lt;?&gt;&gt; messages) {
        <span class="hljs-keyword">return</span> (index, command) -&gt; {
            LOGGER.info(<span class="hljs-string">&quot;Dispatching a command {}.&quot;</span>, command);
            <span class="hljs-keyword">return</span> command;
        };
    }
}
</code></pre>
<p>We can register this dispatch interceptor with a <code>CommandBus</code> by doing the following:</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CommandBusConfiguration</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> CommandBus <span class="hljs-title">configureCommandBus</span><span class="hljs-params">()</span> </span>{
        CommandBus commandBus = SimpleCommandBus.builder().build();
        commandBus.registerDispatchInterceptor(<span class="hljs-keyword">new</span> MyCommandDispatchInterceptor());
        <span class="hljs-keyword">return</span> commandBus;
    }
}
</code></pre>
<h4 id="structural-validation">Structural validation</h4>
<p>There is no point in processing a command if it does not contain all required information in the correct format. In fact, a command that lacks information should be blocked as early as possible, preferably even before a transaction has been started. Therefore, an interceptor should check all incoming commands for the availability of such information. This is called structural validation.</p>
<p>Axon Framework has support for JSR 303 Bean Validation based validation. This allows you to annotate the fields on commands with annotations like <code>@NotEmpty</code> and <code>@Pattern</code>. You need to include a JSR 303 implementation (such as Hibernate-Validator) on your classpath. Then, configure a <code>BeanValidationInterceptor</code> on your command bus, and it will automatically find and configure your validator implementation. While it uses sensible defaults, you can fine-tune it to your specific needs.</p>
<blockquote>
<p><strong>Interceptor Ordering Tip</strong></p>
<p>You want to spend as few resources on an invalid command as possible. Therefore, this interceptor is generally placed at the very front of the interceptor chain. In some cases, a <code>LoggingInterceptor</code> or <code>AuditingInterceptor</code> might need to be placed first, with the validating interceptor immediately following it.</p>
</blockquote>
<p>The <code>BeanValidationInterceptor</code> also implements <code>MessageHandlerInterceptor</code>, allowing you to configure it as a handler interceptor as well.</p>
<h3 id="command-handler-interceptors">Command Handler Interceptors</h3>
<p>Message handler interceptors can take action both before and after command processing. Interceptors can even block command processing altogether, for example for security reasons.</p>
<p>Interceptors must implement the <code>MessageHandlerInterceptor</code> interface. This interface declares one method, <code>handle</code>, that takes two parameters: the current <code>UnitOfWork</code> and an <code>InterceptorChain</code>. The <code>InterceptorChain</code> is used to continue the dispatching process. The <code>UnitOfWork</code> gives you (1) the message being handled and (2) provides the possibility to tie in logic prior, during or after (command) message handling (see <a href="unit-of-work.html">Unit Of Work</a> for more information about the phases).</p>
<p>Unlike dispatch interceptors, handler interceptors are invoked in the context of the command handler. That means they can attach correlation data based on the message being handled to the unit of work, for example. This correlation data will then be attached to messages being created in the context of that unit of work.</p>
<p>Handler interceptors are also typically used to manage transactions around the handling of a command. To do so, register a <code>TransactionManagingInterceptor</code>, which in turn is configured with a <code>TransactionManager</code> to start and commit (or roll back) the actual transaction.</p>
<p>Let&apos;s create a Message Handler Interceptor which will only allow the handling of commands that contain <code>axonUser</code> as a value for the <code>userId</code> field in the <code>MetaData</code>. If the <code>userId</code> is not present in the metadata, an exception will be thrown which will prevent the command from being handled. Additionally, if the value of <code>userId</code> does not match <code>axonUser</code>, we will also not proceed up the chain.</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyCommandHandlerInterceptor</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MessageHandlerInterceptor</span>&lt;<span class="hljs-title">CommandMessage</span>&lt;?&gt;&gt; </span>{

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">handle</span><span class="hljs-params">(UnitOfWork&lt;? extends CommandMessage&lt;?&gt;&gt; unitOfWork, InterceptorChain interceptorChain)</span> <span class="hljs-keyword">throws</span> Exception </span>{
        CommandMessage&lt;?&gt; command = unitOfWork.getMessage();
        String userId = Optional.ofNullable(command.getMetaData().get(<span class="hljs-string">&quot;userId&quot;</span>))
                                .map(uId -&gt; (String) uId)
                                .orElseThrow(IllegalCommandException::<span class="hljs-keyword">new</span>);
        <span class="hljs-keyword">if</span> (<span class="hljs-string">&quot;axonUser&quot;</span>.equals(userId)) {
            <span class="hljs-keyword">return</span> interceptorChain.proceed();
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
    }
}
</code></pre>
<p>We can register the handler interceptor with a <code>CommandBus</code> like so:</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CommandBusConfiguration</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> CommandBus <span class="hljs-title">configureCommandBus</span><span class="hljs-params">()</span> </span>{
        CommandBus commandBus = SimpleCommandBus.builder().build();
        commandBus.registerHandlerInterceptor(<span class="hljs-keyword">new</span> MyCommandHandlerInterceptor());
        <span class="hljs-keyword">return</span> commandBus;
    }

}
</code></pre>
<h4 id="commandhandlerinterceptor-annotation"><code>@CommandHandlerInterceptor</code> Annotation</h4>
<p>The framework has the ability to add a Handler Interceptor as a <code>@CommandHandlerInterceptor</code> annotated method on the Aggregate/Entity.
The difference between a method on an Aggregate and a &quot;<a href="message-intercepting.html#command-handler-interceptors">regular</a>&quot; Command Handler Interceptor, is that with the annotation approach you can make decisions based on the current state of the given Aggregate.
Some properties of an annotated Command Handler Interceptor are:</p>
<ul>
<li>The annotation can be put on entities within the Aggregate.</li>
<li>It is possible to intercept a command on Aggregate Root level, whilst the command handler is in a child entity.</li>
<li>Command execution can be prevented by firing an exception from an annotated command handler interceptor.</li>
<li>It is possible to define an <code>InterceptorChain</code> as a parameter of the command handler interceptor method and use it to control command execution.</li>
<li>By using the <code>commandNamePattern</code> attribute of the <code>@CommandHandlerInterceptor</code> annotation we can intercept all commands matching the provided regular expression.</li>
<li>Events can be applied from an annotated command handler interceptor.</li>
</ul>
<p>In the example below we can see a <code>@CommandHandlerInterceptor</code> annotated method which prevents command execution if a command&apos;s <code>state</code> field does not match the Aggregate&apos;s <code>state</code> field:</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">GiftCard</span> </span>{
    <span class="hljs-comment">//..</span>
    <span class="hljs-keyword">private</span> String state;
    <span class="hljs-comment">//..</span>
    <span class="hljs-meta">@CommandHandlerInterceptor</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">intercept</span><span class="hljs-params">(RedeemCardCommand command, InterceptorChain interceptorChain)</span> </span>{
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.state.equals(command.getState())) {
            interceptorChain.proceed();
        }
    }
}
</code></pre>
<p>Note that the <code>@CommandHandlerInterceptor</code> is essentially a more specific implementation of the <code>@MessageHandlerInterceptor</code> described <a href="#messagehandlerinterceptor">here</a>.</p>
<h2 id="event-interceptors">Event Interceptors</h2>
<p>Similar to command messages, event messages can also be intercepted prior to publishing and handling to perform additional actions on all events. This boils down to the same two types of interceptors for messages: the dispatch interceptor and the handler interceptor.</p>
<h3 id="event-dispatch-interceptors">Event Dispatch Interceptors</h3>
<p>Any message dispatch interceptors registered to an event bus will be invoked when an event is published. They have the ability to alter the event message by adding metadata. They can also provide you with overall logging capabilities for when an event is published. These interceptors are always invoked on the thread that published the event.</p>
<p>Let&apos;s create an event message dispatch interceptor which logs each event message being published on an <code>EventBus</code>.</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EventLoggingDispatchInterceptor</span>
                <span class="hljs-keyword">implements</span> <span class="hljs-title">MessageDispatchInterceptor</span>&lt;<span class="hljs-title">EventMessage</span>&lt;?&gt;&gt; </span>{

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Logger logger =
                LoggerFactory.getLogger(EventLoggingDispatchInterceptor.class);

    <span class="hljs-meta">@Override</span>
    <span class="hljs-keyword">public</span> BiFunction&lt;Integer, EventMessage&lt;?&gt;, EventMessage&lt;?&gt;&gt; handle(
                List&lt;? extends EventMessage&lt;?&gt;&gt; messages) {
        <span class="hljs-keyword">return</span> (index, event) -&gt; {
            logger.info(<span class="hljs-string">&quot;Publishing event: [{}].&quot;</span>, event);
            <span class="hljs-keyword">return</span> event;
        };
    }
}
</code></pre>
<p>We can then register this dispatch interceptor with an <code>EventBus</code> by doing the following:</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EventBusConfiguration</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> EventBus <span class="hljs-title">configureEventBus</span><span class="hljs-params">(EventStorageEngine eventStorageEngine)</span> </span>{
        <span class="hljs-comment">// note that an EventStore is a more specific implementation of an EventBus</span>
        EventBus eventBus = EmbeddedEventStore.builder()
                                              .storageEngine(eventStorageEngine)
                                              .build();
        eventBus.registerDispatchInterceptor(<span class="hljs-keyword">new</span> EventLoggingDispatchInterceptor());
        <span class="hljs-keyword">return</span> eventBus;
    }
}
</code></pre>
<h3 id="event-handler-interceptors">Event Handler Interceptors</h3>
<p>Message handler interceptors can take action both before and after event processing. Interceptors can even block event processing altogether, for example for security reasons.</p>
<p>Interceptors must implement the <code>MessageHandlerInterceptor</code> interface. This interface declares one method, <code>handle()</code>, that takes two parameters: the current <code>UnitOfWork</code> and an <code>InterceptorChain</code>. The <code>InterceptorChain</code> is used to continue the dispatching process. The <code>UnitOfWork</code> gives you (1) the message being handled and (2) provides the possibility to tie in logic prior, during or after (event) message handling (see <a href="unit-of-work.html">Unit Of Work</a> for more information about the phases).</p>
<p>Unlike dispatch interceptors, handler interceptors are invoked in the context of the event handler. That means they can attach correlation data based on the message being handled to the unit of work, for example. This correlation data will then be attached to event messages being created in the context of that unit of work.</p>
<p>Let&apos;s create a message handler interceptor which will only allow the handling of events that contain <code>axonUser</code> as the value for the <code>userId</code> field in the <code>MetaData</code>. If the <code>userId</code> is not present in the metadata, an exception will be thrown which will prevent the Event from being handled. And if the value of <code>userId</code> does not match <code>axonUser</code>, we will also not proceed up the chain. Authenticating the event message like shown in this example is a regular use case of the <code>MessageHandlerInterceptor</code>.</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyEventHandlerInterceptor</span>
        <span class="hljs-keyword">implements</span> <span class="hljs-title">MessageHandlerInterceptor</span>&lt;<span class="hljs-title">EventMessage</span>&lt;?&gt;&gt; </span>{

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">handle</span><span class="hljs-params">(UnitOfWork&lt;? extends EventMessage&lt;?&gt;&gt; unitOfWork,
                         InterceptorChain interceptorChain)</span> <span class="hljs-keyword">throws</span> Exception </span>{
        EventMessage&lt;?&gt; event = unitOfWork.getMessage();
        String userId = Optional.ofNullable(event.getMetaData().get(<span class="hljs-string">&quot;userId&quot;</span>))
                                .map(uId -&gt; (String) uId)
                                .orElseThrow(IllegalEventException::<span class="hljs-keyword">new</span>);
        <span class="hljs-keyword">if</span> (<span class="hljs-string">&quot;axonUser&quot;</span>.equals(userId)) {
            <span class="hljs-keyword">return</span> interceptorChain.proceed();
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
    }
}
</code></pre>
<p>We can register the handler interceptor with an <code>EventProcessor</code> like so:</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EventProcessorConfiguration</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">configureEventProcessing</span><span class="hljs-params">(Configurer configurer)</span> </span>{
        configurer.eventProcessing()
                  .registerTrackingEventProcessor(<span class="hljs-string">&quot;my-tracking-processor&quot;</span>)
                  .registerHandlerInterceptor(<span class="hljs-string">&quot;my-tracking-processor&quot;</span>,
                                              configuration -&gt; <span class="hljs-keyword">new</span> MyEventHandlerInterceptor());
    }
}
</code></pre>
<blockquote>
<p><strong>Interceptor Registration</strong></p>
<p>Different from the <code>CommandBus</code> and <code>QueryBus</code>, which both can have handler interceptors and dispatch interceptors, the <code>EventBus</code> can only register dispatch interceptors. This is because the sole purpose of the <code>EventBus</code> is event publishing/dispatching, thus they are where event dispatch interceptors are registered. <code>EventProcessor</code>s are in charge of handling event messages, thus they are where event handler interceptors are registered.</p>
</blockquote>
<h2 id="query-interceptors">Query Interceptors</h2>
<p>One of the advantages of using a query bus is the ability to undertake action based on all incoming queries. Examples are logging or authentication, which you might want to do regardless of the type of query. This is done using interceptors.</p>
<h3 id="query-dispatch-interceptors">Query Dispatch Interceptors</h3>
<p>Message dispatch interceptors are invoked when a query is dispatched on a query bus or when a subscription update to a query message is dispatched on a query update emitter. They have the ability to alter the message by adding metadata. They can also block the handler execution by throwing an exception. These interceptors are always invoked on the thread that dispatches the message.</p>
<h4 id="structural-validation">Structural validation</h4>
<p>There is no point in processing a query if it does not contain all required information in the correct format. In fact, a query that lacks information should be blocked as early as possible. Therefore, an interceptor should check all incoming queries for the availability of such information. This is called structural validation.</p>
<p>Axon Framework has support for JSR 303 Bean Validation based validation. This allows you to annotate the fields on queries with annotations like <code>@NotEmpty</code> and <code>@Pattern</code>. You need to include a JSR 303 implementation (such as Hibernate-Validator) on your classpath. Then, configure a <code>BeanValidationInterceptor</code> on your query bus, and it will automatically find and configure your validator implementation. While it uses sensible defaults, you can fine-tune it to your specific needs.</p>
<blockquote>
<p><strong>Interceptor Ordering Tip</strong></p>
<p>You want to spend as few resources on an invalid queries as possible. Therefore, this interceptor is generally placed at the very front of the interceptor chain. In some cases, a logging or auditing interceptor might need to be placed first, with the validating interceptor immediately following it.</p>
</blockquote>
<p>The <code>BeanValidationInterceptor</code> also implements <code>MessageHandlerInterceptor</code>, allowing you to configure it as a handler interceptor as well.</p>
<h3 id="query-handler-interceptors">Query Handler Interceptors</h3>
<p>Message handler interceptors can take action both before and after query processing. Interceptors can even block query processing altogether, for example for security reasons.</p>
<p>Interceptors must implement the <code>MessageHandlerInterceptor</code> interface. This interface declares one method, <code>handle</code>, that takes two parameters: the current <code>UnitOfWork</code> and an <code>InterceptorChain</code>. The <code>InterceptorChain</code> is used to continue the dispatching process. The <code>UnitOfWork</code> gives you (1) the message being handled and (2) provides the possibility to tie in logic prior, during or after (query) message handling (see<a href="unit-of-work.html"> Unit Of Work </a>for more information about the phases).</p>
<p>Unlike dispatch interceptors, handler interceptors are invoked in the context of the query handler. That means they can attach correlation data based on the message being handled to the unit of work, for example. This correlation data will then be attached to messages being created in the context of that unit of work.</p>
<h2 id="messagehandlerinterceptor">@MessageHandlerInterceptor</h2>
<p>Alongside defining overall <code>MessageHandlerInterceptor</code> instances on the component handling a message (e.g. a command, query or event), it is also possible to define a handler interceptor for a specific component containing the handlers.
This can be achieved by adding a method handling the message, combined with the <code>@MessageHandlerInterceptor</code> annotation.
Adding such a method allows you more fine-grained control over which message handling components should react and how these should react.</p>
<p>Several handles are given to you when it comes to adding the <code>@MessageHandlerInterceptor</code>, like:</p>
<ol>
<li><code>MessageHandlerInterceptor</code> instances work with the <code>InterceptorChain</code> to decide when to proceed with other interceptors in the chain. The <code>InterceptorChain</code> is an <em>optional</em> parameter which can be added to the intercepting method to provide you with the same control. In absence of this parameter, the framework will call <code>InterceptorChain#proceed</code> once the method is exited.</li>
<li>You can define the type of <code>Message</code> the interceptor should deal with. By default, it reacts to any <code>Message</code> implementation. If an <code>EventMessage</code> specific interceptor is desired, the <code>messageType</code> parameter on the annotation should be set to <code>EventMessage.class</code>.</li>
<li>For even more fine-grained control of which messages should react to the interceptor, the <code>payloadType</code> contained in the <code>Message</code> to handle can be specified.    </li>
</ol>
<p>The following snippets shows some possible approaches of using the <code>@MessageHandlerInterceptor</code> annotation:</p>
<p> tabs 
 tab title=&quot;Simple @MessageHandlerInterceptor method&quot; </p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CardSummaryProjection</span> </span>{
    <span class="hljs-comment">/*
     * Some @EventHandler and @QueryHandler annotated methods
     */</span>
    <span class="hljs-meta">@MessageHandlerInterceptor</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">intercept</span><span class="hljs-params">(Message&lt;?&gt; message)</span> </span>{
        <span class="hljs-comment">// Add your intercepting logic here based on the</span>
    }
}
</code></pre>
<p> endtab </p>
<p> tab title=&quot;@MessageHandlerInterceptor method defining the Message type&quot; </p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CardSummaryProjection</span> </span>{
    <span class="hljs-comment">/*
     * Some @EventHandler and @QueryHandler annotated methods
     */</span>
    <span class="hljs-meta">@MessageHandlerInterceptor</span>(messageType = EventMessage.class)
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">intercept</span><span class="hljs-params">(EventMessage&lt;?&gt; eventMessage)</span> </span>{
        <span class="hljs-comment">// Add your intercepting logic here based on the</span>
    }
}
</code></pre>
<p> tab title=&quot;@MessageHandlerInterceptor method defining the Message and payload type&quot; </p>
<p> endtab </p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CardSummaryProjection</span> </span>{
    <span class="hljs-comment">/*
     * Some @EventHandler and @QueryHandler annotated methods
     */</span>
    <span class="hljs-meta">@MessageHandlerInterceptor</span>(
        messageType = EventMessage.class,
        payloadType = CardRedeemedEvent.class
    )
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">intercept</span><span class="hljs-params">(CardRedeemedEvent event)</span> </span>{
        <span class="hljs-comment">// Add your intercepting logic here based on the</span>
    }
}
</code></pre>
<p> endtab </p>
<p> tab title=&quot;@MessageHandlerInterceptor method defining an InterceptorChain parameter&quot; </p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CardSummaryProjection</span> </span>{
    <span class="hljs-comment">/*
     * Some @EventHandler and @QueryHandler annotated methods
     */</span>
    <span class="hljs-meta">@MessageHandlerInterceptor</span>(messageType = QueryMessage.class)
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">intercept</span><span class="hljs-params">(QueryMessage&lt;?, ?&gt; queryMessage,
                          InterceptorChain interceptorChain)</span> <span class="hljs-keyword">throws</span> Exception </span>{
        <span class="hljs-comment">// Add your intercepting logic before</span>
        interceptorChain.proceed();
        <span class="hljs-comment">// or after the InterceptorChain#proceed invocation</span>
    }
}
</code></pre>
<p> endtab 
 endtabs </p>
<p>Next to the message, payload and <code>InterceptorChain</code>, a <code>@MessageHandlerInterceptor</code> annotated method can resolve other parameters as well.
Which parameters the framework can resolve on such a function, is based on the type of <code>Message</code> being handled by the interceptor.
For more specifics on which parameters are resolvable for the <code>Message</code> being handled, take a look at <a href="supported-parameters-annotated-handlers.html">this</a> page.  </p>
<h3 id="exceptionhandler">@ExceptionHandler</h3>
<p>The <code>@MessageHandlerInterceptor</code> allows for a more specific version of an intercepting function too.
Namely, an <code>@ExceptionHandler</code> annotated method.
A function annotated with <code>@ExceptionHandler</code> will be regarded as a handler interceptor which will <em>only</em> be invoked for exceptional results.
Using annotated functions to this end for example allow you to throw a more domain specific exception as a result of a thrown database/service exception.
You can introduce an <code>@ExceptionHandler</code> which reacts to all exceptions, or specify which exceptions annotated handler reacts on as shown here:</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CardSummaryProjection</span> </span>{

    <span class="hljs-comment">/*
     * Some @EventHandler and @QueryHandler annotated methods
     */</span>
    <span class="hljs-meta">@ExceptionHandler</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handle</span><span class="hljs-params">(Exception exception)</span> </span>{
        <span class="hljs-comment">// How you prefer to react to this generic exception,</span>
        <span class="hljs-comment">//  for example by throwing a domain specific exception.</span>
    }

    <span class="hljs-meta">@ExceptionHandler</span>(resultType = IllegalArgumentException.class)
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handle</span><span class="hljs-params">(IllegalArgumentException exception)</span> </span>{
        <span class="hljs-comment">// How you prefer to react to the IllegalArgumentException,</span>
        <span class="hljs-comment">//  for example by throwing a domain specific exception.</span>
    }
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="message-correlation.html" class="navigation navigation-prev " aria-label="Previous page: Message Correlation">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="supported-parameters-annotated-handlers.html" class="navigation navigation-next " aria-label="Next page: Supported Parameters for Annotated Handlers">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Message Intercepting","level":"3.2.3","depth":2,"next":{"title":"Supported Parameters for Annotated Handlers","level":"3.2.4","depth":2,"path":"axon-framework/messaging-concepts/supported-parameters-annotated-handlers.md","ref":"axon-framework/messaging-concepts/supported-parameters-annotated-handlers.md","articles":[]},"previous":{"title":"Message Correlation","level":"3.2.2","depth":2,"path":"axon-framework/messaging-concepts/message-correlation.md","ref":"axon-framework/messaging-concepts/message-correlation.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"axon-framework/messaging-concepts/message-intercepting.md","mtime":"2021-09-13T08:33:06.087Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-09-13T08:33:36.904Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

